package com.qingfeng.medical.biz.order.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingfeng.medical.biz.order.dao.RefundInfoMapper;
import com.qingfeng.medical.biz.order.domain.entity.PaymentInfo;
import com.qingfeng.medical.biz.order.domain.entity.RefundInfo;
import com.qingfeng.medical.biz.order.domain.enums.RefundStatusEnum;
import com.qingfeng.medical.biz.order.service.RefundInfoService;
import org.springframework.stereotype.Service;

/**
 * 退款信息表
 *
 * @author 清风学Java
 * @date 2023-05-11 13:43:47
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    /**
     * 保存退款记录
     *
     * @param paymentInfo
     * @return
     */
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        //判断是否有重复数据添加
        RefundInfo refundInfo = baseMapper.selectOne(
                new LambdaQueryWrapper<RefundInfo>()
                        .eq(RefundInfo::getOrderId, paymentInfo.getOrderId())
                        .eq(RefundInfo::getPaymentType, paymentInfo.getPaymentType())
        );
        if (ObjectUtil.isNotNull(refundInfo)) {
            return refundInfo;
        }
        // 保存交易记录
        refundInfo = RefundInfo.builder()
                .orderId(paymentInfo.getOrderId())
                .paymentType(paymentInfo.getPaymentType())
                .outTradeNo(paymentInfo.getOutTradeNo())
                .refundStatus(RefundStatusEnum.INIT)
                .subject(paymentInfo.getSubject())
                .totalAmount(paymentInfo.getTotalAmount())
                .build();
        baseMapper.insert(refundInfo);
        return refundInfo;
    }
}